package cn.doctor.assistant.pad.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.TextView;

import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.ui.UI;

/**
 * Created by admin on 2016/2/22.
 */
public class BaseActivity extends FragmentActivity {

    ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        UI.enter(this);
        super.onCreate(savedInstanceState);
    }

    public void showDialogMessage(String paramString) {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setMessage(paramString);
        localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
            }
        });
        localBuilder.create().show();
    }

    public void showDialogMessage(String title, String message) {
        new AlertDialog.Builder(this).setMessage(message).setTitle(title).create().show();
    }

    public void showDialogMessage(String title, String message, boolean cancelable, final Runnable paramRunnable) {
        new AlertDialog.Builder(this).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt) {
                if (paramRunnable != null) {
                    paramRunnable.run();
                }
            }
        }).setCancelable(cancelable).setMessage(message).setTitle(title).create().show();
    }

    public void showProgress(final String paramString) {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            return;
        }
        if (Looper.getMainLooper() == Looper.myLooper()) {
            this.mProgressDialog = ProgressDialog.show(this, null, paramString, true);
            return;
        }
        runOnUiThread(new Runnable() {
            public void run() {
                BaseActivity.this.mProgressDialog = ProgressDialog.show(BaseActivity.this, null, paramString, true);
            }
        });
    }

    public void showProgress(final String msg, final boolean cancelable) {
        if (this.mProgressDialog == null) {
            return;
        }
        if (Looper.getMainLooper() == Looper.myLooper()) {
            ProgressDialog.show(this, null, msg, cancelable);
            return;
        }
        runOnUiThread(new Runnable() {
            public void run() {
                ProgressDialog.show(BaseActivity.this, null, msg, cancelable);
            }
        });
    }

    public void showToast(String msg) {
        UI.showToast(msg);
    }

    public void stopProgress() {
        if (this.mProgressDialog == null) {
            return;
        }
        if (Looper.getMainLooper() == Looper.myLooper()) {
            this.mProgressDialog.dismiss();
            return;
        }
        runOnUiThread(new Runnable() {
            public void run() {
                BaseActivity.this.mProgressDialog.dismiss();
            }
        });
    }

    protected void back() {
        onBackPressed();
    }

    public void setTitle(String title) {
        TextView tvTitle = (TextView) findViewById(R.id.titlebar_left_title);
        if (tvTitle == null) {
            Log.e("jzsn", "布局中未加入titlebar的布局");
            return;
        }
        tvTitle.setText(title);

    }

    public void setRightTitleBarText(String text) {
        TextView tvTitle = (TextView) findViewById(R.id.titlebar_right_text);
        if (tvTitle == null) {
            Log.e("jzsn", "布局中未加入titlebar_right_add或titlebar_right_save布局");
            return;
        }
        tvTitle.setText(text);

    }
}

